import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';

interface Message {
	_id: string;
	role_type_name?: string;
	tablename: string;
	field: string;
	fieldtitle: string;
	search_field: string;
	search_title: string;
	modelid1: string;
	systemid: string;
	productid: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	if (msg.role_type_name) {
		const { exec: e, prepare: p } = count(action_id, session_id, systemid);
		p('pub_role_type', { role_type_name: msg.role_type_name, productid: msg.productid });
		const [_count] = await e();
		if (_count > 0) {
			return 'repert';
		}
	}
	const { update, exec } = operate(action_id, session_id, systemid);
	update('pub_role_type', { productid: msg.productid, _id: msg._id }, { role_type_name: msg.role_type_name, tablename: msg.tablename, field: msg.field, fieldtitle: msg.fieldtitle, search_field: msg.search_field, search_title: msg.search_title, modelid: msg.modelid1, systemid: msg.systemid, productid: msg.productid });
	const res = await exec();
	if (res.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
